package imp;

import dao.InformAgainstDao;
import model.Goods;
import model.InformAgainst;
import model.Trading;
import util.DUBtil;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InformAgainst_imp implements InformAgainstDao{
    @Override
    public Boolean AddInformAgainstInfo(InformAgainst informAgainst) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into informagainst(id,goodsid,userid,goodsname,goodsimgurl,date,state) " +
                "values(?,?,?,?,?,?,?)";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, informAgainst.getId());
            pstmt.setString(2, informAgainst.getGoodsId());
            pstmt.setString(3, informAgainst.getUserId());
            pstmt.setString(4, informAgainst.getGoodsName());
            pstmt.setString(5, informAgainst.getGoodsImgUrl());
            pstmt.setString(6, informAgainst.getDate());
            pstmt.setString(7, informAgainst.getState());
            pstmt.executeUpdate();
            pstmt.close();
            con.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public InformAgainst judgeinfo(String GoodsId,String UserId) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        InformAgainst informAgainst = new InformAgainst();
        String sql = "select * from informagainst where goodsid=? and userid=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, GoodsId);
            pstmt.setString(2, UserId);
            System.out.println("交易记录查找SQL语句:"+pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                informAgainst.setId(rs.getString("id"));
            }
            rs.close();
            pstmt.close();
            con.close();
            return informAgainst;
        } catch (SQLException e) {
            e.printStackTrace();
            return informAgainst;
        }
    }

    @Override
    public List<InformAgainst> ShowInformAgainstInfo(String user_id) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from informagainst where userid=? ORDER BY date DESC";
        List<InformAgainst> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, user_id );
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                InformAgainst informAgainst = new InformAgainst();
                informAgainst.setGoodsId(rs.getString("goodsid"));
                informAgainst.setGoodsName(rs.getString("goodsname"));
                informAgainst.setGoodsImgUrl(rs.getString("goodsimgurl"));
                informAgainst.setDate(rs.getString("date"));
                informAgainst.setState(rs.getString("state"));
                list.add(informAgainst);
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }
    @Override
    public boolean UpdateInformAgainst(String IAid) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstml = null;
        String sql = "update informagainst set state=?where id=?";
        try {
            pstml = connection.prepareStatement(sql);
            pstml.setString(1, "已处理");
            pstml.setString(2, IAid);
            pstml.executeUpdate();
            connection.close();
            pstml.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    public List<InformAgainst> ShowInformAgainstInfo() {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from informagainst ORDER BY date DESC";
        List<InformAgainst> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                InformAgainst informAgainst = new InformAgainst();
                informAgainst.setId(rs.getString("id"));
                informAgainst.setGoodsId(rs.getString("goodsid"));
                informAgainst.setUserId(rs.getString("userid"));
                informAgainst.setGoodsName(rs.getString("goodsname"));
                informAgainst.setGoodsImgUrl(rs.getString("goodsimgurl"));
                informAgainst.setDate(rs.getString("date"));
                informAgainst.setState(rs.getString("state"));
                list.add(informAgainst);
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }
}
